
package chatclient.applets;

import chatclient.applets.note.Note;
import chatclient.applets.canvas.NoteCanvas;
import chatclient.applets.canvas.TabCanvas;
import chatclient.applets.canvas.ViewHeader;
import chatclient.applets.components.Border;
import chatclient.applets.components.Button;
import chatclient.applets.components.Scrollbar;
import chatclient.netio.Attr;
import chatclient.netio.AttrList;
import chatclient.netio.ClientAttr;
import chatclient.netio.Event;
import chatclient.netio.Member;
import chatclient.tools.ChatException;
import chatclient.tools.NoteListener;
import java.awt.Adjustable;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.io.ByteArrayInputStream;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.BorderFactory;
import chatclient.applets.components.FancyTextField;
import java.awt.event.MouseListener;

public class TextPanel extends ChatPanel implements ActionListener, KeyListener, FocusListener {
    
    private ViewHeader[] channelHeaders;
    private NoteCanvas[] channelCanvases;
    private Adjustable[] channelBars;
    private JTextField[] channelTextFields;
    private JPanel[] channelPanels;
    private TabCanvas nameCanvas;
    private NoteCanvas privateCanvas;
    private Adjustable privateBar;
    private JTextField privateTextField;
    private Button privateCloseButton;
    private Button privateIgnoreButton;
    private JPanel ctextPanel;
    private JPanel privatePanel;
    private JTextField lastFocus;
    private Vector history;
    private int maxInputHistory;
    private int maxHistory;
    private int maxChannels;
    private Vector tabNicks;
    private Vector tabHistories;
    private int selectedTab;
    private final String tabDir = "tabs";
    private int channelCount;
    private String[] channelNames;

    private int[] channelSeqNrs;
    private int[] channelCurrents;
    private int[] channelCurrent1s;
    private int[] channelCount1s;

    private String completionBase;
    private Vector inputHistory;
    private int inputCurrent;   
    
    private static Method mtdSetFocusTraversalKeysEnabled;
    private static Method mtdTransferFocusBackward;
    private int fieldMaxSize = 75;

    static {
        try {
            mtdSetFocusTraversalKeysEnabled = Class.forName("java.awt.Component").getMethod("setFocusTraversalKeysEnabled", new Class[]{
                        Boolean.TYPE});

            mtdTransferFocusBackward = Class.forName("java.awt.Component").getMethod("transferFocusBackward", new Class[0]);
        } catch (Exception localException) {
        }
    }    
    
    public TextPanel(TextView parent)
    {
        super(parent);
    }
     
     
    @Override
    protected void initChatView() {

        super.initChatView();
        
        backgroundInfoNr = 2;
        
        String s;
        this.maxInputHistory = ((s = parent.getParameter("maxInputHistory")) == null ? 100 : Integer.parseInt(s));
        this.maxHistory = ((s = parent.getParameter("maxHistory")) == null ? 100 : Integer.parseInt(s));
        this.maxChannels = ((s = parent.getParameter("maxChannels")) == null ? 1 : Integer.parseInt(s));
        this.inputHistory = new Vector();
        this.inputHistory.addElement("");
        this.inputCurrent = 0;
        this.channelCount = 1;
        this.channelNames = new String[this.maxChannels];
        this.channelHeaders = new ViewHeader[this.maxChannels];
        this.channelSeqNrs = new int[this.maxChannels];
        this.channelCurrents = new int[this.maxChannels];
        this.channelCurrent1s = new int[this.maxChannels];
        this.channelCount1s = new int[this.maxChannels];
        this.channelCanvases = new NoteCanvas[this.maxChannels];
        this.channelBars = new Adjustable[this.maxChannels];
        this.channelTextFields = new JTextField[this.maxChannels];
        this.channelPanels = new JPanel[this.maxChannels];
        Border border = (s = parent.getParameter("channelHeaderBorder")) == null ? null : parent.createBorder(s);
        for (int i = 0; i < this.maxChannels; i++) {
            this.channelNames[i] = null;
            this.channelPanels[i] = new JPanel();
            this.channelPanels[i].setBackground(getBackground());
            this.channelPanels[i].setLayout(new BorderLayout());
            if ((s = parent.getParameter("channelHeader")) != null) {
                this.channelHeaders[i] = new ViewHeader(parent, "header", s, border);
                this.channelHeaders[i].setBackground(getBackground());
                this.channelHeaders[i].setForeground(getForeground());
                this.channelHeaders[i].setFont(getFont());
                this.channelPanels[i].add(this.channelHeaders[i], "North");
            }
            this.channelBars[i] = parent.createScrollbar();
            this.channelPanels[i].add((JComponent) this.channelBars[i], "East");
            this.channelCanvases[i] = new NoteCanvas(parent, "channel", false);
            this.channelCanvases[i].setBackground(getBackground());
            this.channelCanvases[i].setForeground(getForeground());
            this.channelCanvases[i].setFont(getFont());
            this.channelPanels[i].add(this.channelCanvases[i], "Center");
            
            for (int ii = 0; ii < channelCanvases.length; ii++)
            {
                channelCanvases[ii].addMouseListener(new MouseListener() {

                    @Override
                    public void mouseClicked(MouseEvent e) {
                        System.out.println("ADZ - Clicked - Source child clicked = ");
                        
                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet.");
                    }

                    @Override
                    public void mouseExited(MouseEvent e) {
                        //throw new UnsupportedOperationException("Not supported yet.");
                    }
                });
            }
            
            JPanel chatpanel = new JPanel();
            chatpanel.setBackground(getBackground());
            chatpanel.setLayout(new BorderLayout());
            if ((s = parent.getParameter("inputLabel")) != null) {
                ViewHeader viewheader = new ViewHeader(parent, "inputLabel", s, false);
                viewheader.setBackground(getBackground());
                viewheader.setForeground(getForeground());
                viewheader.setFont(getFont());
                viewheader.setAttrs(new AttrList());
                chatpanel.add(viewheader, "West");
            }
            this.channelTextFields[i] = new FancyTextField(fieldMaxSize);//new JTextField();
            this.channelTextFields[i].setBackground((s = parent.getParameter("inputBgcolor")) == null ? Color.white : Color.decode(s));
            this.channelTextFields[i].setForeground((s = parent.getParameter("inputFgcolor")) == null ? Color.black : Color.decode(s));
            this.channelTextFields[i].addActionListener(this);
            this.channelTextFields[i].addKeyListener(this);
            this.channelTextFields[i].addFocusListener(this);
            chatpanel.add(this.channelTextFields[i], "Center");
            if (mtdSetFocusTraversalKeysEnabled != null) {
                try {
                    mtdSetFocusTraversalKeysEnabled.invoke(this.channelTextFields[i], new Object[]{
                                false});
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
            this.channelPanels[i].add(chatpanel, "South");
            this.channelBars[i].addAdjustmentListener(this.channelCanvases[i]);
        }

        this.privatePanel = new JPanel();
        this.privatePanel.setBackground(getBackground());
        this.privatePanel.setLayout(new BorderLayout());
        this.nameCanvas =
                new TabCanvas((TextView)parent, "tab", new Image[]{
                    getTabImage("topLeft"), getTabImage("left"), getTabImage("bottomLeft"), getTabImage("top"), getTabImage("bottom"), getTabImage("topRight"), getTabImage("right"), getTabImage("bottomRight"), getTabImage("topLeftSelected"), getTabImage("leftSelected"),
                    getTabImage("bottomLeftSelected"), getTabImage("topSelected"), getTabImage("bottomSelected"), getTabImage("topRightSelected"), getTabImage("rightSelected"), getTabImage("bottomRightSelected")},
                new Image[]{
                    getTabImage("topSpace"), getTabImage("space"), getTabImage("bottomSpace")});

        this.nameCanvas.setBackground(getBackground());
        this.nameCanvas.setForeground(getForeground());
        this.nameCanvas.setFont(getFont());
        this.privatePanel.add(this.nameCanvas, "North");
        this.privateBar = parent.createScrollbar();
        this.privatePanel.add((JComponent) this.privateBar, "East");
        this.privateCanvas = new NoteCanvas(parent, "private", false);
        this.privateCanvas.setBackground(getBackground());
        this.privateCanvas.setForeground(getForeground());
        this.privateCanvas.setFont(getFont());
        this.privatePanel.add(this.privateCanvas, "Center");
        JPanel chatpanel1 = new JPanel();
        chatpanel1.setBackground(getBackground());
        chatpanel1.setLayout(new BorderLayout());
        if ((s = parent.getParameter("inputLabel")) != null) {
            ViewHeader viewheader1 = new ViewHeader(parent, "inputLabel", s, false);
            viewheader1.setBackground(getBackground());
            viewheader1.setForeground(getForeground());
            viewheader1.setFont(getFont());
            viewheader1.setAttrs(new AttrList());
            chatpanel1.add(viewheader1, "West");
        }
        this.privateTextField = new FancyTextField(fieldMaxSize);
        this.privateTextField.setBackground((s = parent.getParameter("inputBgcolor")) == null ? Color.white : Color.decode(s));
        this.privateTextField.setForeground((s = parent.getParameter("inputFgcolor")) == null ? Color.black : Color.decode(s));
        this.privateTextField.addActionListener(this);
        this.privateTextField.addKeyListener(this);
        this.privateTextField.addFocusListener(this);
        if (mtdSetFocusTraversalKeysEnabled != null) {
            try {
                mtdSetFocusTraversalKeysEnabled.invoke(this.privateTextField, new Object[]{
                            false});
            } catch (Exception exception1) {
                exception1.printStackTrace();
            }
        }
        chatpanel1.add(this.privateTextField, "Center");
        JPanel chatpanel2 = new JPanel();
        chatpanel2.setBackground(getBackground());
        chatpanel2.setLayout(new FlowLayout(2, 0, 0));
        this.privateIgnoreButton = parent.createButton("ignorePrivate");
        this.privateIgnoreButton.addActionListener(this);
        chatpanel2.add(this.privateIgnoreButton);
        this.privateCloseButton = parent.createButton("closePrivate");
        this.privateCloseButton.addActionListener(this);
        chatpanel2.add(this.privateCloseButton);
        chatpanel1.add(chatpanel2, "East");
        this.privatePanel.add(chatpanel1, "South");
        this.privateBar.addAdjustmentListener(this.privateCanvas);
        this.ctextPanel = new JPanel();
        this.ctextPanel.setBackground(getBackground());
        updateTextPanel();
        add(this.ctextPanel, "Center");
        updateBars();
    }

    @Override
    protected void cleanupChatView() {
        for (int i = 0; i < this.channelNames.length; i++) {
            if ((this.channelBars[i] != null) && ((this.channelBars[i] instanceof Scrollbar))) {
                this.channelBars[i].removeAdjustmentListener(this.channelCanvases[i]);
                ((Scrollbar) this.channelBars[i]).destroy();
                this.channelBars[i] = null;
            }
            if (this.channelCanvases[i] != null) {
                this.channelCanvases[i].destroy();
            }
        }
        if ((this.privateBar != null) && ((this.privateBar instanceof Scrollbar))) {
            this.privateBar.removeAdjustmentListener(this.privateCanvas);
            ((Scrollbar) this.privateBar).destroy();
            this.privateBar = null;
        }
        if (this.privateCanvas != null) {
            this.privateCanvas.destroy();
        }
        super.cleanupChatView();
    }

    private void updateTextPanel() {
        boolean flag = (this.tabNicks != null) && (!this.tabNicks.isEmpty());
        this.ctextPanel.removeAll();
        this.ctextPanel.setLayout(new GridLayout(this.channelCount + (flag ? 1 : 0), 1));
        for (int i = 0; i < this.channelCount; i++) {
            this.ctextPanel.add(this.channelPanels[i]);
        }
        if (flag) {
            this.ctextPanel.add(this.privatePanel);
        }
        this.ctextPanel.validate();
        setAtBottom();
        if ((this.lastFocus != null) && (this.lastFocus.isShowing())) {
            this.lastFocus.requestFocus();
        }
    }

    private int findTab(String s) {
        int i = -1;
        if (s != null) {
            for (i = 0; i < this.tabNicks.size(); i++) {
                if (s.equalsIgnoreCase((String) this.tabNicks.elementAt(i))) {
                    break;
                }
            }
            if (i >= this.tabNicks.size()) {
                i = -1;
            }
        }
        return i;
    }

    private void createTab(String s, boolean flag) {
        Member member = parent.getMember(s);
        if (member == null) {
            try {
                AttrList attrlist = new AttrList();
                attrlist.add(new ClientAttr(ClientAttr.NICKNAME, s));
                member = new Member(s, attrlist);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
        if (flag) {
            this.tabNicks.addElement(s);
            this.tabHistories.addElement(new Vector());
        }
        this.nameCanvas.addTab(parent.createMemberNote(this.nameCanvas, member));
        if (this.selectedTab < 0) {
            updateTextPanel();
            selectTab(0);
        }
    }

    private void closeTab(int i) {
        Member member = parent.getMember((String) this.tabNicks.elementAt(i));
        if (member != null) {
            member.setMarked(false);
        }
        this.nameCanvas.removeTab(i);
        this.tabNicks.removeElementAt(i);
        this.tabHistories.removeElementAt(i);
        if (this.tabNicks.isEmpty()) {
            updateTextPanel();
            this.channelTextFields[0].requestFocus();
        }
        if (i == this.selectedTab) {
            selectTab(-1);
        }
        if (i >= this.selectedTab) {
            selectTab(Math.min(i, this.tabNicks.size() - 1));
        }
    }

    private void selectTab(int i) {
        Member member = i < 0 ? null : parent.getMember((String) this.tabNicks.elementAt(i));
        if (member != null) {
            member.setMarked(false);
        }
        if (this.selectedTab != i) {
            this.selectedTab = i;
            this.privateCanvas.removeAllNotes();
            if ((this.selectedTab >= 0) && (this.selectedTab < this.tabHistories.size())) {
                displayAllNotes((Vector) this.tabHistories.elementAt(this.selectedTab), true);
            }
        }

        this.nameCanvas.repaint();
        updateBar(this.privateBar, this.privateCanvas);
    }

    private void displayAllNotes(Vector vector, boolean flag) {
        Event event;
        for (Enumeration enumeration = vector.elements(); enumeration.hasMoreElements(); displayNote(event.getId(), null, event.getParams(), false, flag, false, 0)) {
            event = (Event) enumeration.nextElement();
        }
    }

    private void updateBars() {
        for (int i = 0; i < this.channelCount; i++) {
            updateBar(this.channelBars[i], this.channelCanvases[i]);
        }
        updateBar(this.privateBar, this.privateCanvas);
    }

    private void updateBar(Adjustable adjustable, NoteCanvas notecanvas) {
        int i = notecanvas.getTop();
        int j = notecanvas.getVisible();
        int k = notecanvas.getAverageNoteHeight();
        if ((adjustable instanceof Scrollbar)) {
            ((Scrollbar) adjustable).setValues(i, j, notecanvas.getMinimum(), notecanvas.getMaximum());
        } else if ((adjustable instanceof Scrollbar)) {
            ((Scrollbar) adjustable).setValues(i, j, notecanvas.getMinimum(), notecanvas.getMaximum());
        } else {
            adjustable.setMinimum(notecanvas.getMinimum());
            adjustable.setMaximum(notecanvas.getMaximum());
            adjustable.setVisibleAmount(j);
            adjustable.setValue(i);
        }
        adjustable.setBlockIncrement(j);
        adjustable.setUnitIncrement(k);
    }

    public Image getTabImage(String s) {
        return parent.getImage("tabs", s);
    }

    public void setAtBottom() {
        updateBars();
        for (int i = 0; i < this.channelCount; i++) {
            this.channelCanvases[i].setBottom(this.channelCanvases[i].getMaximum());
        }
        this.privateCanvas.setBottom(this.privateCanvas.getMaximum());
        updateBars();
    }

    
    /*
    @Override
    public void repaint() {

        super.repaint();
        
        for (int i = 0; i < this.channelCount; i++) {
            if (this.channelCanvases[i] != null) {
                this.channelCanvases[i].repaint();
            }
        }
        if (this.privateCanvas != null) {
            this.privateCanvas.repaint();
        }
        if (this.nameCanvas != null) {
            this.nameCanvas.repaint();
        }
    }
    */    
 
   private void updateChannelHeader(int i) {
        if (this.channelHeaders[i] == null) {
            return;
        }
        AttrList attrlist;
        try {
            attrlist = (AttrList) parent.getChat().getChannelInfo(this.channelNames[i]).clone();
        } catch (Exception exception) {
            attrlist = null;
        }
        if (attrlist == null) {
            attrlist = new AttrList();
        }
        try {
            attrlist.set(new ClientAttr(ClientAttr.NICKNAME, parent.getChat().getMyName()));
            if (this.channelNames[i] != null) {
                attrlist.set(new ClientAttr(ClientAttr.CHANNELNAME, this.channelNames[i]));
            }
            if ((this.channelCurrents[i] != -1) && (this.channelSeqNrs[i] != -1)) {
                int j = attrlist.get(ClientAttr.MEMBERCOUNT) == null ? -1 : attrlist.get(ClientAttr.MEMBERCOUNT).getInt();
                int k;
                if ((this.channelCurrent1s[i] > 0) && (this.channelCount1s[i] > 0)) {
                    k = (int) ((this.channelSeqNrs[i] - this.channelCurrents[i]) * this.channelCount1s[i] / (this.channelSeqNrs[i] - this.channelCurrent1s[i]));
                } else {
                    k = this.channelSeqNrs[i] - this.channelCurrents[i];
                }
                if (k < 0) {
                    k = 0;
                }
                if (k > j) {
                    k = j;
                }
                attrlist.set(new ClientAttr(ClientAttr.QUEUE_SEQNR, k));
            }
        } catch (Exception exception1) {
            attrlist = null;
        }
        this.channelHeaders[i].setAttrs(attrlist);
    }    

    private Vector createNotes(NoteListener notelistener, byte byte0, String s, String s1, AttrList attrlist, int flag) {
        
        System.out.println("ADZ - TextPanel.java->createNotes::"+notelistener+"+::"+s+"::"+s1+"::"+attrlist);
        Vector vector = new Vector();
        Note note = createNote(notelistener, byte0, s, s1, attrlist, flag);
        if (note != null) {
            vector.addElement(note);
        }
        if (byte0 == Event.LOG) {
            ClientAttr clientattr = null;
            for (Enumeration enumeration = attrlist.getEnumeration(); enumeration.hasMoreElements();) {
                ClientAttr clientattr1 = (ClientAttr) enumeration.nextElement();
                switch (clientattr1.getId()) {
                    default:
                        break;
                    case ClientAttr.LOG_TIME:
                        clientattr = clientattr1;
                        break;
                    case ClientAttr.LOG_EVENT:
                        try {
                            Event event = new Event(new ByteArrayInputStream(clientattr1.getValue()));
                            AttrList attrlist1 = event.getParams();
                            if (clientattr != null) {
                                attrlist1.set(clientattr);
                                clientattr = null;
                            }
                            Note note1 = createNote(notelistener, event.getId(), s, s1, attrlist1, flag);
                            if (note1 == null) {
                                continue;
                            }
                            vector.addElement(note1);
                        } catch (Exception localException) {
                        }
                }
            }
        }
        return vector;
    }

    private Note createNote(NoteListener notelistener, byte byte0, String s, String s1, AttrList attrlist, int flag) {
        System.out.println("ADZ - TextPanel.java->createNote::"+attrlist);
        return s1 == null ? Note.fromEvent(notelistener, parent, size().width, new Event(byte0, attrlist), s, getFont(), getFontMetrics(getFont())) : Note.fromTemplate(notelistener, parent, size().width, s1, null, attrlist, getFont(), getFontMetrics(getFont()), flag);
    }

    private void displayNote(byte eventId, String parameter, AttrList attrlist, boolean flag, boolean flag1, boolean ignored, int flag2) {
    
        System.out.println("ADZ - TextPanel.java->displayNote::"+eventId+"::"+parameter+"::"+attrlist);
        String targetName = null;
        String channelName = null;
        if (parameter == null) {
            try {
                ClientAttr clientattr;
                if ((clientattr = (ClientAttr) attrlist.get(ClientAttr.CHANNELNAME)) != null) {
                    channelName = clientattr.getString();
                }
                if ((clientattr = (ClientAttr) attrlist.get(ClientAttr.TARGETNAME)) != null) {
                    targetName = clientattr.getString();
                    if (targetName.equalsIgnoreCase(parent.getMyName())) {
                        targetName = null;
                    }
                }
                if ((targetName == null) && ((clientattr = (ClientAttr) attrlist.get(ClientAttr.NICKNAME)) != null)) {
                    targetName = clientattr.getString();
                    if (targetName.equalsIgnoreCase(parent.getMyName())) {
                        targetName = null;
                    }
                }
            } catch (Exception exception) {
                targetName = null;
            }
        }
        int i = findTab(targetName);
        if ((ignored) && (i < 0)) {
            return;
        }
        boolean isNicknameNote = (eventId == Event.NOTE) && (attrlist.get(ClientAttr.TARGETNAME) != null) && (channelName == null);
        boolean isChannelNote = (eventId == Event.NOTE) && (channelName != null);
        if ((isNicknameNote) && (targetName != null) && (i < 0) && (flag)) {
            if (!parent.getChat().getPrivateWindows()) {
                isNicknameNote = false;
                isChannelNote = true;
            } else {
                createTab(targetName, true);
                i = this.tabNicks.size() - 1;
            }
        }
        if ((i >= 0) && (i != this.selectedTab)) {
            Member member = parent.getMember(targetName);
            if ((member != null) && (isNicknameNote)) {
                member.setMarked(true);
                this.nameCanvas.repaint();
            }
        }
        if ((i >= 0) && (i == this.selectedTab) && (!isChannelNote)) {
            displayNotes(createNotes(this.privateCanvas, eventId, parent.getSuspendKey() + "private", parameter, attrlist, flag2), this.privateCanvas, flag ? this.privateBar : null);
        }
        if ((!isNicknameNote) && (!flag1)) {
            int j = 0;
            if (channelName != null) {
                for (j = this.channelCount - 1; (j > 0) && ((this.channelNames[j] == null) || (!this.channelNames[j].equals(channelName))); j--) {
                }
            }
            NoteCanvas notecanvas = this.channelCanvases[j];
            Adjustable adjustable = this.channelBars[j];
            displayNotes(createNotes(notecanvas, eventId, parent.getSuspendKey() + "channel", parameter, attrlist, flag2), notecanvas, flag ? adjustable : null);
        }
        if ((parameter == null) && (flag)) {
            this.history.addElement(new Event(eventId, attrlist));
            if (this.history.size() > this.maxHistory) {
                this.history.removeElementAt(0);
            }
            if ((i >= 0) && (!isChannelNote)) {
                Vector vector = (Vector) this.tabHistories.elementAt(i);
                vector.addElement(new Event(eventId, attrlist));
                if (vector.size() > this.maxHistory) {
                    vector.removeElementAt(0);
                }
            }
        }
    }

    private void displayNotes(Vector vector, NoteCanvas notecanvas, Adjustable adjustable) {
        for (Enumeration enumeration = vector.elements(); enumeration.hasMoreElements(); notecanvas.addNote((Note) enumeration.nextElement())) {
            if (notecanvas.getNoteCount() >= this.maxHistory) {
                notecanvas.removeNote(0);
            }
        }
        if ((adjustable != null) && (vector.size() > 0)) {
            updateBar(adjustable, notecanvas);
        }
    }    
   
    
    @Override
    public void actionPerformed(ActionEvent actionevent) {
        try {
            Object obj = actionevent.getSource();
            if ((obj instanceof JTextField)) {
                String s = ((JTextField) obj).getText();
                this.inputCurrent = (this.inputHistory.size() - 1);
                this.inputHistory.setElementAt(s, this.inputCurrent++);
                this.inputHistory.addElement("");
                if (this.inputHistory.size() > this.maxInputHistory) {
                    this.inputHistory.removeElementAt(0);
                    this.inputCurrent -= 1;
                }
                for (int i = 0; i < this.channelCount; i++) {
                    if (obj == this.channelTextFields[i]) {
                        parent.getChat().input(this.channelNames[i], s, false, null);
                        this.channelTextFields[i].setText("");
                        setAtBottom();
                        return;
                    }
                }

                if ((obj == this.privateTextField) && (this.selectedTab >= 0)
                        && (this.selectedTab < this.tabNicks.size())) {
                    parent.getChat().input(null, s, true,
                            (String) this.tabNicks.elementAt(this.selectedTab));
                    this.privateTextField.setText("");
                    setAtBottom();
                }
            } else if (obj == this.privateCloseButton) {
                if ((this.selectedTab >= 0) && (this.selectedTab < this.tabNicks.size())) {
                    parent.getChat().endPrivate(
                            (String) this.tabNicks.elementAt(this.selectedTab));
                }
            } else if ((obj == this.privateIgnoreButton) && (this.selectedTab >= 0)
                    && (this.selectedTab < this.tabNicks.size())) {
                parent.getChat().setIgnoreNick(
                        (String) this.tabNicks.elementAt(this.selectedTab), true);
                parent.getChat().endPrivate((String) this.tabNicks.elementAt(this.selectedTab));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    @Override
    public void keyPressed(KeyEvent keyevent) {
        if ((keyevent.getSource() instanceof JTextField)) {
            JTextField textfield = (JTextField) keyevent.getSource();
            String s = textfield.getText();
            switch (keyevent.getKeyCode()) {
                case 9:
                    byte byte0 = (byte) (keyevent.isShiftDown() ? -1 : 1);
                    int i = textfield.getCaretPosition();
                    int j = Math.max(0, s.substring(0, i).lastIndexOf(' ') + 1);
                    if (j >= i) {
                        break;
                    }
                    String s1 = s.substring(j, i);
                    int k = parent.findMember(s1);
                    Member member = parent.getMember(k + (this.completionBase != null ? byte0 : 0));
                    if (this.completionBase == null) {
                        this.completionBase = s1;
                    }
                    if ((member == null) || (!member.getName().toLowerCase().startsWith(this.completionBase.toLowerCase()))) {
                        member = null;
                        int l = parent.findMember(this.completionBase);
                        for (Member member1 = parent.getMember(l); (member1 != null) && (member1.getName().toLowerCase().startsWith(this.completionBase.toLowerCase())); member1 = parent.getMember(l -= byte0)) {
                            member = member1;
                        }
                    }
                    if ((member != null) && (!member.getName().equals(s1))) {
                        textfield.setText(s.substring(0, j) + member.getName() + s.substring(i));
                        textfield.setCaretPosition(j + member.getName().length());
                        keyevent.consume();
                    } else if (byte0 > 0) {
                        textfield.transferFocus();
                        keyevent.consume();
                    } else {
                        if ((byte0 >= 0) || (mtdTransferFocusBackward == null)) {
                            break;
                        }
                        try {
                            mtdTransferFocusBackward.invoke(textfield, new Object[0]);
                        } catch (Exception exception) {
                            exception.printStackTrace();
                        }
                        keyevent.consume();
                    }

                    break;
                case 38:
                    if (this.inputCurrent > 0) {
                        this.inputHistory.setElementAt(s, this.inputCurrent);
                        textfield.setText((String) this.inputHistory.elementAt(--this.inputCurrent));
                        textfield.setCaretPosition(textfield.getText().length());
                        keyevent.consume();
                    }
                    this.completionBase = null;
                    break;
                case 40:
                    if (this.inputCurrent < this.inputHistory.size() - 1) {
                        this.inputHistory.setElementAt(s, this.inputCurrent);
                        textfield.setText((String) this.inputHistory.elementAt(++this.inputCurrent));
                        textfield.setCaretPosition(textfield.getText().length());
                        keyevent.consume();
                    }
                    this.completionBase = null;
                    break;
                default:
                    if (keyevent.getKeyChar() == 0) {
                        break;
                    }
                    this.completionBase = null;
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent keyevent) {
    }

    @Override
    public void keyTyped(KeyEvent keyevent) {
    }    
    
    @Override
    public void focusGained(FocusEvent focusevent) {
        if ((focusevent.getSource() instanceof JTextField)) {
            this.lastFocus = ((JTextField) focusevent.getSource());
        }
    }

    @Override
    public void focusLost(FocusEvent focusevent) {
        if ((focusevent.getSource() instanceof JTextField)) {
            this.lastFocus = ((JTextField) focusevent.getSource());
        }
    }    
    
    

    public void clearState() {
        this.history = new Vector();
        this.channelNames = new String[this.maxChannels];
        this.tabNicks = new Vector();
        this.tabHistories = new Vector();
        this.selectedTab = -1;
        updateTextPanel();
    }

    public void initState() {
        this.history = new Vector();
        this.channelNames = new String[this.maxChannels];
        this.tabNicks = new Vector();
        this.tabHistories = new Vector();
        this.selectedTab = -1;
        updateTextPanel();
    }


    public void saveState(Vector vector) {
        vector.addElement(this.history);
        vector.addElement(this.channelNames);
        vector.addElement(this.tabNicks);
        vector.addElement(this.tabHistories);
        vector.addElement(new Integer(this.selectedTab));
    }


    public void restoreState(Enumeration enumeration) {
        this.history = ((Vector) enumeration.nextElement());
        this.channelNames = ((String[]) enumeration.nextElement());
        this.tabNicks = ((Vector) enumeration.nextElement());
        this.tabHistories = ((Vector) enumeration.nextElement());
        updateTextPanel();
        displayAllNotes(this.history, false);
        this.selectedTab = -1;
        for (Enumeration enumeration1 = this.tabNicks.elements(); enumeration1.hasMoreElements(); createTab((String) enumeration1.nextElement(), false)) {
        }
        selectTab(((Integer) enumeration.nextElement()).intValue());
    }    
    

    protected void doEvent(Event event, boolean ignored, AttrList attrlist, boolean isNicknameSet, String nickname,
            boolean isMeSource, boolean isTargetNameSet, String targetName, boolean isMeTarget, boolean isChannelNameSet, String channelName, boolean isMyActiveChannel) {
      
        System.out.println("ADZ - TextPanel.java->doEvent:Event="+event+":AttrList="+attrlist);
        switch (event.getId()) {
            case Event.JOINED:
            case Event.PARTED:
            case Event.CHANNELINFO:
            case Event.QUEUEPOSITION:
            case Event.QUEUEUPDATE:
            case Event.ENQUEUED:
            case Event.DEQUEUED:
                for (int i = 0; i < this.channelCount; i++) {
                    if ((this.channelNames[i] == null) || (!this.channelNames[i].equals(channelName))) {
                        continue;
                    }
                    try {
                        switch (event.getId()) {
                            case Event.KILLED: // unused
                            case Event.BANNED: // unused
                            case Event.UNBANNED: // unused
                            case Event.GAVEPERMS: // unused
                            case Event.TOOKPERMS: // unused
                            case Event.ENQUEUED:
                            default:
                                break;
                            case Event.DEQUEUED:
                                this.channelSeqNrs[i] = -1;
                                this.channelCurrents[i] = -1;
                                this.channelCurrent1s[i] = -1;
                                this.channelCount1s[i] = -1;
                                break;
                            case Event.QUEUEPOSITION:
                                this.channelSeqNrs[i] = attrlist.get(ClientAttr.QUEUE_SEQNR).getInt();
                                break;
                            case Event.QUEUEUPDATE:
                                this.channelCurrents[i] = attrlist.get(ClientAttr.QUEUE_CURRENT).getInt();
                                if (this.channelCurrent1s[i] > 0) {
                                    break;
                                }
                                try {
                                    this.channelCurrent1s[i] = this.channelCurrents[i];
                                    this.channelCount1s[i] = parent.getChat().getChannelInfo(this.channelNames[i]).get(ClientAttr.MEMBERCOUNT).getInt();
                                } catch (Exception exception) {
                                    this.channelCurrent1s[i] = -1;

                                    this.channelCount1s[i] = -1;
                                }
                        }
                        updateChannelHeader(i);
                    } catch (Exception exception1) {
                        exception1.printStackTrace();
                    }

                }

        }

        if ((isMeSource) && (event.getId() == Event.JOINED)) {
            if ((this.channelCount == 1) && (this.channelNames[0] == null)) {
                this.channelCount = 0;
            }
            if (this.channelCount < this.channelNames.length) {
                this.channelNames[this.channelCount] = channelName;
                updateChannelHeader(this.channelCount);
                this.channelCount += 1;
                if (this.channelCount > 1) {
                    updateTextPanel();
                    this.channelTextFields[(this.channelCount - 1)].requestFocus();
                }
            }
        }
        displayNote(event.getId(), null, event.getParams(), true, false, ignored, 0);
        if ((isMeSource) && (event.getId() == Event.PARTED)) {
            for (int j = 0; j < this.channelCount; j++) {
                if ((this.channelNames[j] == null) || (!this.channelNames[j].equals(channelName))) {
                    continue;
                }
                if (j < this.channelCount - 1) {
                    String s3 = this.channelNames[j];
                    JPanel chatpanel = this.channelPanels[j];
                    ViewHeader viewheader = this.channelHeaders[j];
                    int k = this.channelSeqNrs[j];
                    int l = this.channelCurrents[j];
                    int i1 = this.channelCurrent1s[j];
                    int j1 = this.channelCount1s[j];
                    Adjustable adjustable = this.channelBars[j];
                    JTextField textfield = this.channelTextFields[j];
                    NoteCanvas notecanvas = this.channelCanvases[j];
                    System.arraycopy(this.channelNames, j + 1, this.channelNames, j, this.channelCount - (j + 1));
                    System.arraycopy(this.channelPanels, j + 1, this.channelPanels, j, this.channelCount - (j + 1));
                    System.arraycopy(this.channelHeaders, j + 1, this.channelHeaders, j, this.channelCount - (j + 1));
                    System.arraycopy(this.channelSeqNrs, j + 1, this.channelSeqNrs, j, this.channelCount - (j + 1));
                    System.arraycopy(this.channelCurrents, j + 1, this.channelCurrents, j, this.channelCount - (j + 1));
                    System.arraycopy(this.channelCurrent1s, j + 1, this.channelCurrent1s, j, this.channelCount - (j + 1));
                    System.arraycopy(this.channelCount1s, j + 1, this.channelCount1s, j, this.channelCount - (j + 1));
                    System.arraycopy(this.channelBars, j + 1, this.channelBars, j, this.channelCount - (j + 1));
                    System.arraycopy(this.channelTextFields, j + 1, this.channelTextFields, j, this.channelCount - (j + 1));
                    System.arraycopy(this.channelCanvases, j + 1, this.channelCanvases, j, this.channelCount - (j + 1));
                    this.channelNames[(this.channelCount - 1)] = s3;
                    this.channelPanels[(this.channelCount - 1)] = chatpanel;
                    this.channelHeaders[(this.channelCount - 1)] = viewheader;
                    this.channelSeqNrs[(this.channelCount - 1)] = k;
                    this.channelCurrents[(this.channelCount - 1)] = l;
                    this.channelCurrent1s[(this.channelCount - 1)] = i1;
                    this.channelCount1s[(this.channelCount - 1)] = j1;
                    this.channelBars[(this.channelCount - 1)] = adjustable;
                    this.channelTextFields[(this.channelCount - 1)] = textfield;
                    this.channelCanvases[(this.channelCount - 1)] = notecanvas;
                }
                this.channelNames[(this.channelCount - 1)] = null;
                if (this.channelCount <= 1) {
                    break;
                }
                this.channelCanvases[(this.channelCount - 1)].removeAllNotes();
                this.channelCount -= 1;
                updateTextPanel();
                this.channelTextFields[0].requestFocus();

                break;
            }
        }
    }

 


    public void doInternalEvent(String parameter, AttrList attrlist) {
        
        int flag = 0;
        Attr flagAttr = attrlist.get(ClientAttr.VARVALUE);
        if(flagAttr != null)
        {
          flag = flagAttr.getByte();
        }

        displayNote(Event.NUL, parent.getParameter(parameter), attrlist, true, false, false, flag);
    }



    public void updatedBgimage(Image image) {

        for (int i = 0; i < this.channelCount; i++) {
            this.channelCanvases[i].setImage(image);
        }
        this.privateCanvas.setImage(image);
    }


    public void startedPrivate(String s) {
        int i = findTab(s);
        if (i < 0) {
            createTab(s, true);
        }
        i = findTab(s);
        if (i >= 0) {
            selectTab(i);
        }
    }


    public void endedPrivate(String s) {
        int i = findTab(s);
        if (i >= 0) {
            closeTab(i);
        }
    }


    public void clearedNotes(String s) {
        this.history.removeAllElements();
        for (int i = 0; i < this.channelCount; i++) {
            if (((s == null) && (this.channelNames[i] == null)) || ((s != null) && (this.channelNames[i] != null) && (this.channelNames[i].equals(s)))) {
                this.channelCanvases[i].removeAllNotes();
            }
        }
        updateBars();
    }    
    
    public void selectedTab(int i) {
        selectTab(i);
    }

    public int getSelectedTab() {
        return this.selectedTab;
    }

    public void componentResized(ComponentEvent componentevent) {
        setAtBottom();
    } 
}
